local Msg = require("core.Msg")
local SignConfig = require("excel.Fuli").sign
local FuliDefine = require("fuli.FuliDefine")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Grid = require("bag.Grid")
local MonthCardLogic = require("vip.MonthCardLogic")
local Util = require("common.Util")
local Handler = require("fuli.HandlerL")
local Log = require("common.Log")

--检查是否可领取奖励
function checkShowTips(human)
  for i=1, #SignConfig do
    if human.db.fuliIsGet[FuliDefine.FULI_TYPE_VIP_SIGN * 15 + i - 1] == FuliDefine.REWARD_CAN_GET then
      return 1
    end
  end
  return 0
end

function getRewardList(human)
  local sendMsg = Msg.gc.GC_FULI_REWARD
  local cache = {}
  sendMsg.list[0] = #SignConfig 
  for i=1, #SignConfig do
    sendMsg.list[i].page = FuliDefine.FULI_TYPE_SIGN
    sendMsg.list[i].canGet = human.db.fuliIsGet[FuliDefine.FULI_TYPE_SIGN * 15 + i - 1]
    sendMsg.list[i].vipCanGet = human.db.fuliIsGet[FuliDefine.FULI_TYPE_VIP_SIGN * 15 + i - 1]
    sendMsg.list[i].condition = SignConfig[i].sign
    sendMsg.list[i].needYuanbao = 0
    sendMsg.list[i].reward[0] = #SignConfig[i].reward
    for j=1, #SignConfig[i].reward do
      Grid.create(cache, SignConfig[i].reward[j][1], SignConfig[i].reward[j][2])
      Grid.makeItem(cache, sendMsg.list[i].reward[j], 0, human)
    end   
    sendMsg.list[i].vipReward[0] = #SignConfig[i].vipReward
    for j=1, #SignConfig[i].vipReward do
      Grid.create(cache, SignConfig[i].vipReward[j][1], SignConfig[i].vipReward[j][2])
      Grid.makeItem(cache, sendMsg.list[i].vipReward[j], 0, human)
    end  
    sendMsg.list[i].onlineTime = 0
    sendMsg.list[i].percent = 0
  end
  Msg.send(sendMsg, human.fd)
  sendSignList(human)  
end

function getIndex(human, condition)
  for i=1, #SignConfig do
    if SignConfig[i].sign == condition then
      return i
    end
  end
  return 0
end

function getReward(human, condition, isVipReward)
  local index = getIndex(human, condition)  
  if index == 0 then
    return
  end
  if isVipReward ~= 1 then
    if human.db.fuliIsGet[FuliDefine.FULI_TYPE_SIGN * 15 + index - 1] ~= FuliDefine.REWARD_CAN_GET then
      return
    end
    if not BagLogic.checkCanAddItemList(human, SignConfig[index].reward,#SignConfig[index].reward) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_BAG_NOT_ENOUGH)
    end
    human.db.fuliIsGet[FuliDefine.FULI_TYPE_SIGN * 15 + index - 1] = FuliDefine.REWARD_ALREADY_GET
    for i=1, #SignConfig[index].reward do
      BagLogic.addItem(human, SignConfig[index].reward[i][1], 1, SignConfig[index].reward[i][2], "fulidating")
    end        
  else
    if not MonthCardLogic.isHaveSpec(human) then
      return
    end    
    if human.db.fuliIsGet[FuliDefine.FULI_TYPE_VIP_SIGN * 15 + index - 1] ~= FuliDefine.REWARD_CAN_GET then
      return
    end
    if not BagLogic.checkCanAddItemList(human, SignConfig[index].vipReward,#SignConfig[index].vipReward) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_BAG_NOT_ENOUGH)
    end
    human.db.fuliIsGet[FuliDefine.FULI_TYPE_VIP_SIGN * 15 + index - 1] = FuliDefine.REWARD_ALREADY_GET
    for i=1, #SignConfig[index].vipReward do
      BagLogic.addItem(human, SignConfig[index].vipReward[i][1], 1, SignConfig[index].vipReward[i][2], "fulidating")
    end     
  end
  getRewardList(human)
  Handler.checkShowTips(human)
end

function sendSignList(human)
  local sendMsg = Msg.gc.GC_FULI_SIGN_LIST
  sendMsg.isSign[0] = tonumber(os.date("%d",os.time({year=os.date("%Y"),month=os.date("%m")+1,day=0})))  
  
  for i=1,sendMsg.isSign[0] do
    sendMsg.isSign[i] = human.db.signDay[i - 1]
  end
  Msg.send(sendMsg, human.fd)
end

function checkCanGet(human)
  local maxDay = os.date("%d",os.time({year=os.date("%Y"),month=os.date("%m")+1,day=0}))
  local signDays = 0
  for i=1, maxDay do
    if human.db.signDay[i - 1] == 1 then
      signDays = signDays + 1      
    end
  end
  for i=1, #SignConfig do
    if signDays >= SignConfig[i].sign then
      if human.db.fuliIsGet[FuliDefine.FULI_TYPE_SIGN * 15 + i - 1] == FuliDefine.REWARD_NOT_CAN_GET then
        human.db.fuliIsGet[FuliDefine.FULI_TYPE_SIGN * 15 + i - 1] = FuliDefine.REWARD_CAN_GET
      end
      if human.db.fuliIsGet[FuliDefine.FULI_TYPE_VIP_SIGN * 15 + i - 1] == FuliDefine.REWARD_NOT_CAN_GET and MonthCardLogic.isHaveSpec(human) then
        human.db.fuliIsGet[FuliDefine.FULI_TYPE_VIP_SIGN * 15 + i - 1] = FuliDefine.REWARD_CAN_GET
      end
    end
  end
  getRewardList(human)
  sendSignList(human)
end

function sign(human, day)
  local date = os.date("*t", os.time())
  day = date.day
  human.db.signDay[day - 1] = 1
  checkCanGet(human)
  Handler.checkShowTips(human)
  
  local maxDay = os.date("%d",os.time({year=os.date("%Y"),month=os.date("%m")+1,day=0}))
  local signDays = 0
  for i=1, maxDay do
    if human.db.signDay[i - 1] == 1 then
      signDays = signDays + 1      
    end
  end
  Log.write(Log.LOGID_OSS_DAILY_SIGN, human._id, human.account, human.name,human.db.lv, signDays)
end

function updateStatus(human)
--  if not Util.isSameMonth(human.db.update_daily_time) then
--    for i=0, 30 do
--      human.db.signDay[i] = 0
--    end
--    for i=1, #SignConfig do
--      human.db.fuliIsGet[FuliDefine.FULI_TYPE_SIGN * 15 + i - 1] = FuliDefine.REWARD_NOT_CAN_GET
--      human.db.fuliIsGet[FuliDefine.FULI_TYPE_VIP_SIGN * 15 + i - 1] = FuliDefine.REWARD_NOT_CAN_GET
--    end
--    getRewardList(human)
--    sendSignList(human)
--  end  
end